<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <meta name="HandheldFriendly" content="true" />
    <meta name="MobileOptimized" content="320" />
    <title>网络错误</title>
    <style>
      * {
        -webkit-user-select: none;
      }
      html,
      body {
        margin: 0;
        padding: 0;
        font-family: PingFang SC, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto,
          Hiragino Sans GB, Microsoft YaHei UI, Microsoft YaHei, Source Han Sans CN, sans-serif,
          Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol;
        background: #f5f5f5;
      }
      .container {
        padding-top: 20%;
        text-align: center;
      }
      .container img {
        display: block;
        width: 108px;
        height: 108px;
        margin: 0 auto;
      }
      .container h2 {
        margin-top: 16px;
        margin-bottom: 8px;
        color: rgba(0, 0, 0, 0.88);
        line-height: 1.3;
        font-size: 20px;
        font-weight: 500;
      }
      .container p {
        margin-top: 8px;
        margin-bottom: 8px;
        color: rgba(0, 0, 0, 0.45);
        line-height: 1.375;
        font-size: 16px;
        font-weight: 400;
      }
      .container .button {
        background-color: #1677ff;
        color: #fff;
        border: 0;
        margin-top: 24px;
        padding: 10px 16px;
        border-radius: 8px;
        line-height: 1.3;
        font-size: 15px;
        font-weight: 500;
      }
      .container .button:active {
        background-color: #0958d9;
      }
    </style>
    <script
      type="text/javascript"
      src="https://hellouniapp.dcloud.net.cn/hybrid/html/uni.webview.1.5.5.js"
    ></script>
  </head>
  <body>
    <div class="container">
      <img
        src=""
        alt=""
      />
      <h2>网络错误</h2>
      <p>很抱歉网络开小差，请稍候再试</p>
      <!--<div class="button" onclick="history.back()">Retry</div>-->
      <!-- <div class="button back" onclick="backClick()">返回</div> -->
      <button class="button back" onclick="closeClick()">确定</button>
      <!-- <button class="button reload" onclick="restartClick()">刷新</button> -->
    </div>

    <script type="text/javascript">
      var ws = null;
      var ab_ = {
        plusReady(callback) {
          if (window.plus) {
            callback();
          } else {
            document.addEventListener('plusready', callback);
          }

          document.addEventListener(
            'touchstart',
            function () {
              return false;
            },
            true,
          );

          // 禁止选择
          document.oncontextmenu = function () {
            return false;
          };

          // 获取错误信息
          document.addEventListener(
            'error',
            function (e) {
              console.log('请求的页面无法打开：' + e.href);
            },
            false,
          );

          // 不回调
          document.addEventListener(
            'close',
            function () {
              console.log('请求的页面已关闭');
            },
            false,
          );

          // 不回调
          document.addEventListener(
            'hide',
            function () {
              console.log('请求的页面已隐藏');
            },
            false,
          );

          try {
            // 初始化webview
            window.uniWebview = uni;
          } catch (e) {}
        },
      };

      ab_.plusReady(function () {
        var native;
        var center;
        try {
          ws = plus.webview.currentWebview();
          native = plus.os.name.toLowerCase();
          if (native === 'android') {
            center = plus.android.importClass('com.absir.uniplugin.AbCenter').ME();
          } else if (plus.ios) {
            center = plus.ios.importClass('AbCenter').ME();
          }
        } catch (e) {
          console.error(e);
        }

        ab_.native = native;
        ab_.center = center;
        ab_.postEvent = function (event, paras) {
          if (center) {
            if (native === 'android') {
              center.postEvent(event, paras);
            } else {
              plus.ios.invoke(center, 'postEvent:paras:', event, paras);
            }
            return true;
          }
        };

        plus.key.addEventListener(
          'backbutton',
          function () {
            console.log('backbutton');
            window.uniWebview.postMessage({
              data: {
                service: 'errorPage', // 模块
                action: 'close', // 关闭
              },
            });
            ab_.postEvent('message', 'backProj');
            ws.close();
            // history.length == 1 && ws.close();
            // var c = setTimeout(function () {
            //   ws.close();
            // }, 1000);
            // window.onbeforeunload = function () {
            //   clearTimeout(c);
            // };
            // history.go(-2);
          },
          false,
        );

        // 告诉webview当前是错误页,直接关闭即可
        // window.uniWebview.postMessage({
        //   data: {
        //     service: 'errorPage', // 模块
        //     action: 'closePending', // 关闭表示
        //   },
        // });
      });

      // 返回
      function backClick() {
        // history.length == 1 && closeClick();
        // var c = setTimeout(function () {
        //   closeClick();
        // }, 1000);
        // window.onbeforeunload = function () {
        //   clearTimeout(c);
        // };
        // history.go(-2);
        // if (history.length == 1) {
        //   ws.close();
        // } else {
        //   ws.back();
        //   ws.back();
        // }
      }

      // 关闭
      function closeClick() {
        console.log('closeClick');
        window.uniWebview.postMessage({
          data: {
            service: 'errorPage', // 模块
            action: 'close', // 关闭
          },
        });
        ab_.postEvent('message', 'backProj');
        ws.close();
      }

      // 重启
      function restartClick() {
        // plus.runtime.restart();
        window.uniWebview.postMessage({
          data: {
            service: 'errorPage', // 模块
            action: 'refresh', // 刷新
          },
        });
      }
    </script>
  </body>
</html>
